import { Vector } from '../vector'
import {Person, PersonWidth, PersonHeight, GenerationSpace, CoupleLineHeight, CouplePadding} from './person'
import {Line} from './line'

//一对，可能有两个人(夫妻)，可能有一个人，也可能没人
//女的在左边，男的在右边
class Couple {
  //pos一个Vector（上面中点位置）, data是后台返回的couple数据。
  //data例子：{"child":8,"child_male":1,"family":1,"husband":10000,"husband_name":"汪军","parent":1,"priority":1,"wife":10024,"wife_name":"齐春"}
  constructor(tree_drawer, pos, data, options = {}) {
    //自身的大小
    this.tree_drawer = tree_drawer
    this.data = data
    this.pos = pos.Copy()
    this.options = options  //绘制参数
    this.width = 0
    this.height = 0

    //根据有几个人，来估计位置。生成wife和husband对象。
    this.wife = null
    this.husband = null
    this.lines = []  //夫妻连接线, Line数组
    if (data.wife != 0 && data.husband != 0) {
      //两个人
      this.width = PersonWidth * 2 + CouplePadding
      this.height = PersonHeight

      this.wife = new Person(this.tree_drawer, pos.Copy().Sub(new Vector(CouplePadding/2 + PersonWidth/2, 0)), data.wife_name, data.wife)
      this.husband = new Person(this.tree_drawer, pos.Copy().Add(new Vector(CouplePadding/2 + PersonWidth/2, 0)), data.husband_name, data.husband)

      //初始化夫妻连接线
      {
        //p0     p1
        //p2     p3
        let p0 = this.pos.Copy().Add(new Vector(-CouplePadding/2 - PersonWidth/2, this.height))
        let p2 = this.pos.Copy().Add(new Vector(-CouplePadding/2 - PersonWidth/2, this.height + CoupleLineHeight))
        let p1 = this.pos.Copy().Add(new Vector(CouplePadding/2 + PersonWidth/2, this.height))
        let p3 = this.pos.Copy().Add(new Vector(CouplePadding/2 + PersonWidth/2, this.height + CoupleLineHeight))
        this.lines.push(new Line(tree_drawer, p0, p2))
        this.lines.push(new Line(tree_drawer, p1, p3))
        this.lines.push(new Line(tree_drawer, p2, p3))
      }

    } else if (data.wife == 0 && data.husband == 0) {
      //无人
      this.width = 0
      this.height = 0

    } else {
      //一个人
      this.width = PersonWidth
      this.height = PersonHeight

      if (data.wife != 0) {
        this.wife = new Person(this.tree_drawer, pos, data.wife_name, data.wife)
      } else {
        this.husband = new Person(this.tree_drawer, pos, data.husband_name, data.husband)
      }
    }
  }

  IsEmpty() {
    return this.data.wife == 0 && this.data.husband == 0
  }

  //绘制函数
  Draw() {
    if (this.wife) {
      this.wife.Draw()
    }

    if (this.husband) {
      this.husband.Draw()
    }

    //画夫妻连接线
    for (let i = 0; i < this.lines.length; i++) {
      this.lines[i].Draw()
    }
  }

  //获取固定大小, 不包含连接线
  GetFixedSize() {
    return new Vector(this.width, this.height)
  }

  //获取和长辈连接的位置， 返回一个Vector对象，可能为null
  GetParentConnectPos() {
    if (this.data.wife == 0 && this.data.husband == 0) {
      return null
    }
    if (this.data.wife != 0 && this.data.husband != 0) {
      if (this.data.child_male) {
        //丈夫有长辈
        return this.pos.Copy().Add(new Vector(CouplePadding/2 + PersonWidth/2, 0))
      } else {
        //妻子有长辈
        return this.pos.Copy().Sub(new Vector(CouplePadding/2 + PersonWidth/2, 0))
      }
    } else {
      return this.pos.Copy()
    }
  }

  //获取和后代连接的位置
  GetChildConnectPos() {
    if (this.data.wife == 0 && this.data.husband == 0) {
      return null
    }
    if (this.data.wife != 0 && this.data.husband != 0) {
      //返回妻子 + 丈夫
      return this.pos.Copy().Add(new Vector(0, this.height + CoupleLineHeight))
    } else {
      return this.pos.Copy().Add(new Vector(0, this.height))
    }
  }

  //移动
  Move(vec) {
    if (vec.IsZero()) {
      return
    }
    this.pos.Add(vec)
    if (this.wife) {
      this.wife.Move(vec)
    }
    if (this.husband) {
      this.husband.Move(vec)
    }
    for (let i = 0; i < this.lines.length; i++) {
      this.lines[i].Move(vec)
    }
  }

  //获取某个位置(vector)选中的人，返回Person对象，没有选中返回null
  GetSelectPerson(pos) {
    if (this.wife && this.wife.IsInRange(pos)) {
      return this.wife
    }
    if (this.husband && this.husband.IsInRange(pos)) {
      return this.husband
    }
    return null
  }
}

module.exports = {
  Couple
}